//
//  ANShowInfoTableView.m
//  AttitudeNews
//
//  Created by 刘晓萌 on 15/11/24.
//  Copyright © 2015年 刘晓萌. All rights reserved.
//

#import "ANShowInfoTableView.h"
#import "DataHelper.h"
#import "ANNewsModel.h"
#import "Urls.h"

#import "ANShowNewsViewController.h"
#import "ANNewsListViewController.h"
#import "ANSliderPicCell.h"
#import "ANNormalCell.h"
#import "ANPicsCell.h"
#import "ANLongPicCell.h"

#import "MJRefresh.h"

@interface ANShowInfoTableView ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)DataHelper *DH;
@property(strong,nonatomic)NSMutableArray *dataArray;
@property (strong, nonatomic) MJRefreshNormalHeader *mjHeader;
@property (strong, nonatomic) MJRefreshBackNormalFooter *mjFooter;
@end

@implementation ANShowInfoTableView

#pragma mark --- 类的基本初始化,注销

/**
 * 注销
 */
-(void)dealloc{
    self.mjFooter = nil;
    self.mjHeader = nil;
    self.DH = nil;
    [self removeObserver:self forKeyPath:@"dataArray"];
}

/**
 * 自定义初始化方法
 */
-(instancetype)initWithFrame:(CGRect)frame andTitleName:(NSString *)titleName{
    if (self = [super initWithFrame:frame]) {
        self.titleName = titleName;
        self.DH = [[DataHelper alloc]init];
        [self setView];
        [self makeData];
    }
    return self;
}

/**
 * 类方法初始化
 */
+(instancetype)instanceWithFrame:(CGRect)frame andTitleName:(NSString *)titleName{
    ANShowInfoTableView *anTable = [[ANShowInfoTableView alloc]initWithFrame:frame andTitleName:titleName];
    return anTable;
}

/**
 * 重写titleName的方法
 */
-(void)setTitleName:(NSString *)titleName{
    if (_titleName != titleName) {
        _titleName = nil;
        _titleName = titleName;
    }
}

#pragma mark --- 视图
/**
 * 设置tableView初始化跟设置代理
 */
-(void)setView{
    self.showInfoTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Width, Height) style:UITableViewStyleGrouped];
    self.showInfoTableView.delegate = self;
    self.showInfoTableView.dataSource = self;
    [self setRefreshForTable];
    [self addSubview:self.showInfoTableView];
    [self registCell];
}

/**
 * 设置刷新第三方,及刷新任务
 */
-(void)setRefreshForTable{
    __weak typeof (self.showInfoTableView)  myTableView = self.showInfoTableView;
    __weak typeof(self) weakSelf = self;
    self.mjHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf makeData];
        [self.mjHeader endRefreshing];
        self.mjHeader.automaticallyChangeAlpha = YES;
    }];
    self.mjFooter = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.DH.ResultBlock = ^(NSMutableArray *arr){
            weakSelf.dataArray = arr;
            [myTableView reloadData];
            [weakSelf.mjFooter endRefreshing];
        };
        NSInteger count = pageShowNumber;
        [self.DH getnewsModelSFromTitle:weakSelf.titleName andPageNumber:weakSelf.dataArray.count+count];
        self.mjFooter.automaticallyChangeAlpha = YES;
    }];
    
    [self.showInfoTableView addSubview:self.mjHeader];
    [self.showInfoTableView addSubview:self.mjFooter];
}

/**
 * 对XIB画成的cell进行注册
 */
-(void)registCell{
    //轮播图cell
    [self.showInfoTableView registerNib:[UINib nibWithNibName:@"ANSliderPicCell" bundle:nil] forCellReuseIdentifier:@"ANSliderPicCell"];
    //普通cell
    [self.showInfoTableView registerNib:[UINib nibWithNibName:@"ANNormalCell" bundle:nil] forCellReuseIdentifier:@"ANNormalCell"];
    //图集cell
    [self.showInfoTableView registerNib:[UINib nibWithNibName:@"ANPicsCell" bundle:nil] forCellReuseIdentifier:@"ANPicsCell"];
    //长图cell
    [self.showInfoTableView registerNib:[UINib nibWithNibName:@"ANLongPicCell" bundle:nil] forCellReuseIdentifier:@"ANLongPicCell"];
}
#pragma mark --- tableView的代理方法

/**
 * 设置section个数
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

/**
 * 设置cell的个数
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

/**
 * 设置每行返回的cell
 */
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ANNewsModel *newsModel = [[ANNewsModel alloc]init];
    newsModel = self.dataArray[indexPath.row];
    
    /**
     * 处理普通Cell
     */
    if (newsModel.cellStyle == cellNormal) {
        ANNormalCell *normalCell = [tableView dequeueReusableCellWithIdentifier:@"ANNormalCell" forIndexPath:indexPath];
        normalCell.newsModel = newsModel;
        return normalCell;
    }
    
    /**
     * 处理图集Cell
     */
    if (newsModel.cellStyle == cellPIC) {
        ANPicsCell *picsCell = [tableView dequeueReusableCellWithIdentifier:@"ANPicsCell" forIndexPath:indexPath];
        picsCell.newsModel = newsModel;
        return picsCell;
    }
    
    
    /**
     * 处理长图Cell
     */
    if (newsModel.cellStyle == cellOnePic) {
        ANLongPicCell *longPicCell = [tableView dequeueReusableCellWithIdentifier:@"ANLongPicCell" forIndexPath:indexPath];
        longPicCell.newsModel = newsModel;
        return longPicCell;
    }
    
    /**
     * 处理轮播图Cell
     */
    if (newsModel.cellStyle == cellHead) {
        ANSliderPicCell *sliderCell = [tableView dequeueReusableCellWithIdentifier:@"ANSliderPicCell" forIndexPath:indexPath];
        sliderCell.newsModel = newsModel;
        [sliderCell setview];
        
        [sliderCell setPushBlock:^(ANNewsModel *newsModel){
            self.PushBlock(newsModel);
        }];
        return sliderCell;
    }
    
    return [[UITableViewCell alloc]init];
}

/**
 * 设置tableView的cell的高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ANNewsModel *newsModel = [[ANNewsModel alloc]init];
    newsModel = self.dataArray[indexPath.row];
    
    /**
     * 处理轮播图Cell的高度
     */
    if (newsModel.cellStyle == cellHead) {
        return 200;
    }
    
    /**
     * 处理图集Cell的高度
     */
    if (newsModel.cellStyle == cellPIC) {
        return 130;
    }
    
    /**
     * 处理长图Cell的高度
     */
    if (newsModel.cellStyle == cellOnePic) {
        return 180;
    }
    
    return 90;
}

/**
 *设置分割线顶格
 */
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

/**
 * 处理tableView的点击事件
 */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ANTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    ANNewsModel *newsModel = cell.newsModel;
    
    self.PushBlock(newsModel);
}



#pragma mark --- 数据获取和处理

/**
 * 通过DataHelper的代理获得数据并处理
 */
-(void)makeData{
    
    [self addObserver:self forKeyPath:@"dataArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];

    __weak typeof(self) weakSelf = self;
    __weak typeof (self.showInfoTableView)  myTableView = self.showInfoTableView;
    self.DH.ResultBlock = ^(NSMutableArray *arr){
        weakSelf.dataArray = arr;
        [myTableView reloadData];
    };
    NSInteger pageCount = pageShowNumber;
    [self.DH getnewsModelSFromTitle:weakSelf.titleName andPageNumber:pageCount];
}

//观察者
//观察者观察到有值发生改变的时候发生的方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //change 字典中的old new 是关键字，专门用来存储新值和老值
    NSArray *arr = (NSArray *)[change objectForKey:@"new"];
    if (arr.count == 0) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"暂无更多内容" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        self.dataArray = [change objectForKey:@"old"];

    }
}



@end
